package com.bill99.schedule;

import com.bill99.model.EmailProperties;
import com.bill99.service.IpoTrackerWorkOrderDelayService;
import com.bill99.util.DateUtil;
import com.bill99.util.EmailUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/***
 * class_name: SendIpoTrackerExcelEmailTask
 * package: com.bill99.schedule
 * creat_user:JunLee
 * creat_date: 2018/8/6
 * creat_time: 15:24
 * describe: 每天早上8点发送ipo-tracker邮件
 */


@Component
public class SendIpoTrackerExcelEmailTask {

    @Autowired
    private EmailUtil emailUtil;

    @Autowired
    private IpoTrackerWorkOrderDelayService service;
    @Autowired
    private EmailProperties emailProperties;
    @Value("${ipo.tracker.excel.uploadPath}")
    private String uploadPath;
    private static final  Logger LOGGER = LoggerFactory.getLogger(SendIpoTrackerExcelEmailTask.class);
    private static final String IPO_TRCKER_TEMPLATE="ipo-tracker-woorkbook.vxls";

   /*** 
    * describe: TODO 
    * creat_time: 2018/8/6 15:23
    * param: []
    * return: void
    */
    @Scheduled(cron = "0 00 18 * * ?")
    public void executeInternal(){

        LOGGER.info("SendIpoTrackerExcelEmailTask execute start.");
        try {
            //查询生成excel
            String excelPath = service.generateExcel(IPO_TRCKER_TEMPLATE, uploadPath);
            LOGGER.info("SendIpoTrackerExcelEmailTask upload excel success.");
            sendEmail(emailProperties,excelPath);
            LOGGER.info("SendIpoTrackerExcelEmailTask send email success.");

        } catch (Exception e) {

            LOGGER.error("SendIpoTrackerExcelEmailTask | execute | Exception",e);
            throw new RuntimeException();
        }finally {
            LOGGER.info("SendIpoTrackerExcelEmailTask execute end.");
        }
    }

    private void sendEmail(EmailProperties emailProperties,String filename) {
        LOGGER.info(emailProperties.toString());
        Map<String, String> params = new HashMap();
        params.put("subject", emailProperties.getSubject());
        params.put("product", emailProperties.getProduct());
        params.put("type", emailProperties.getType());
        params.put("level", emailProperties.getLevel());
        params.put("keyword", emailProperties.getKeyword());
        params.put("time", DateUtil.formatDateTime("yyyy-MM-dd HH:mm:ss",new Date()));
        params.put("desc", emailProperties.getDesc());
        params.put("proposal", emailProperties.getProposal());

        params.put("emailAddresses", emailProperties.getEmailAddresses());//收件人列表，多个用','隔开
        params.put("templateId",emailProperties.getTemplateId());//模板ID
        params.put("attachments", emailProperties.getAttachmentsPrefix()+filename);
        params.put("sendEmailUrl",emailProperties.getInterfaceUrl());

        emailUtil.send(params);
    }

    public static void main(String[] args) {
        EmailUtil emailUtil=new EmailUtil();
        Map<String, String> params = new HashMap();
//        params.put("subject", "subject");
//        params.put("product", "product");
//        params.put("type", "type");
//        params.put("level", "level");
//        params.put("keyword", "keyword");
//        params.put("time", DateUtil.formatDateTime("yyyy-MM-dd HH:mm:ss",new Date()));
//        params.put("desc", "desc");
//        params.put("proposal", "proposal");

        params.put("emailAddresses", "DTS_APP@99bill.com");//收件人列表，多个用','隔开
//        params.put("emailAddresses", "MANAGE_DASHBOARD@99bill.com");//收件人列表，多个用','隔开
        params.put("templateId","19714657");//模板ID
        params.put("attachments","http://192.168.15.164:8888/ipoTrackerExcel/a.jpg");
        params.put("sendEmailUrl","http://192.168.127.38:8084/email-prepare/sendEmail.do");
        emailUtil.send(params);
    }


}
